home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------
- filename Imo.c
- function Imo Joke INIT code resource
- author 1991.4.6.-4.14. K.Nakagawa
- change
- note Developed on Think C ver.4.0.2
- This version is valid only for International Use
- No guarantees or waranties of any kind To use this software
- Copyleft or Copyright is reserved by K.Nakagawa
- (NIFTY serve PAG02107,IKU-NET(KOBE Japan) 287)
- --------------------------------------------------------------------------*/
-
- /*--------------------------------------------------------------------------
- include file
- --------------------------------------------------------------------------*/
-
- static __GetA4(void);
- #include <SetUpA4.h>
- #include <oops.h>
-
- /*--------------------------------------------------------------------------
- definition of constant
- --------------------------------------------------------------------------*/
-
- #define NULL ((void *)0)
-
- /* ID number of STR resource */
- #define START_POINT_ID 1001 /* start position of strings */
- #define VERTICAL_PITCH_ID 1002 /* CR pitch of strings */
- #define DELAY_TIME_ID 1003 /* wait time after 1 line displayed */
- #define BLINK_TIME_ID 1004 /* blink time of cursor */
- #define CURSOR_SIZE_ID 1005 /* size of cursor */
- #define MESSAGE_START_ID 1100 /* start message ID number */
-
- /*--------------------------------------------------------------------------
- definition of types
- --------------------------------------------------------------------------*/
-
- typedef struct
- {
- BitMap *sBitMap; /* bitmap definition of source */
- int left; /* left position of image to escape */
- int top; /* top position of image to escape */
- int right; /* right position of image to escape */
- int bottom; /* bottom position of image to escape */
- BitMap dBitMap; /* bitmap definition of destination */
- } SaveBitMapType;
-
- typedef struct QuickDraw
- {
- char private[76];
- long randSeed;
- BitMap screenBits;
- Cursor arrow;
- Pattern dkGray;
- Pattern ltGray;
- Pattern gray;
- Pattern black;
- Pattern white;
- GrafPtr thePort;
- } QuickDraw;
-
- /*
- private member of TextDrawClass
- aXpos = horizontal position of character,
- aYpos = vertical position of character,
- aVpitch = linefeed pitch
-
- public member of TextDrawClass
- Init(position of character,linefeed pitch) = setup the object
- Perform(ID number of STR resource) = perform joke
- 0=OK,-1=error occurred
- */
- struct TextDrawClass : indirect {
- /* private: */
- int aXpos,aYpos,aVpitch;
- void Scanf(void);
- /* public: */
- void Init(Point pStartPos,int pVpitch);
- int Perform(int pID);
- };
-
- /*
- private member of BlinkCursorClass
- aInvert = if true,object is inverted
- aNextTick = time limit of current state
- aBlinkTime = blink period
- aXsize,aYsize = size of cursor
- public member
- Init(blink period,size of cursor) = setup the object
- Idle() = show blink of cursor
- Hide() = hide cursor
- */
- struct BlinkCursorClass : indirect {
- /* private: */
- Boolean aInvert;
- long aNextTick;
- int aBlinkTime;
- int aXsize,aYsize;
- void Invert(void);
- /* public: */
- void Init(int pBlinkTime,Point pSize);
- void Idle(void);
- void Hide(void);
- };
-
- /*--------------------------------------------------------------------------
- global variables
- --------------------------------------------------------------------------*/
-
- static QuickDraw *gQD; /* pointer to own QuickDraw enviroment */
-
- /*--------------------------------------------------------------------------
- prototype definition
- --------------------------------------------------------------------------*/
-
- void main(void);
- void RealMain(void);
- static Boolean IsShiftPress(void);
- int SaveBitMap(SaveBitMapType *pSaveBitMap);
- void RestoreBitMap(SaveBitMapType *pSaveBitMap);
- static int SetupOffBitMap(BitMap *pBitMap,int ph,int pv);
- static void DisposOffBitMap(BitMap *pBitMap);
- int GetPreset1(int pID);
- Point GetPreset2(int pID);
- static int GetPresetSub(Handle pHandle,int pSkip);
- static void Wait(void);
-
- /*--------------------------------------------------------------------------
- program definition which uses "define" keyword
- --------------------------------------------------------------------------*/
-
- /*--------------------------------------------------------------------------
- BlinkCursorClass
- --------------------------------------------------------------------------*/
-
- void BlinkCursorClass::Init(int pBlinkTime,Point pSize)
- {
- /* local variables */
-
- /* procedures */
- aBlinkTime = pBlinkTime;
- aXsize = pSize.h;
- aYsize = pSize.v;
- aInvert = false;
- Invert();
- }
-
- void BlinkCursorClass::Idle(void)
- {
- /* local variables */
-
- /* procedures */
- if(aNextTick < TickCount())
- Invert();
- }
-
- void BlinkCursorClass::Hide(void)
- {
- /* local variables */
-
- /* procedures */
- if(aInvert)
- Invert();
- }
-
- void BlinkCursorClass::Invert(void)
- {
- /* local variables */
- Point aPenCursor;
- Rect aRect;
-
- /* procedures */
- GetPen(&aPenCursor);
- SetRect(&aRect,
- aPenCursor.h,
- aPenCursor.v - aYsize,
- aPenCursor.h + aXsize,
- aPenCursor.v);
- InvertRect(&aRect);
- aInvert = !aInvert;
- aNextTick = TickCount() + (long)aBlinkTime;
- }
-
- /*--------------------------------------------------------------------------
- TextDrawClass
- --------------------------------------------------------------------------*/
-
- void TextDrawClass::Init(Point pStartPos,int pVpitch)
- {
- aXpos = pStartPos.h;
- aYpos = pStartPos.v;
- aVpitch = pVpitch;
- }
-
- int TextDrawClass::Perform(int pID)
- {
- /* local variables */
- StringHandle aStringHandle;
- StringPtr aStringPtr;
- int aStringLength;
- char aChar;
- Boolean aCRflag = false,aYenFlag = false;
-
- /* procedures */
- if(NULL == (aStringHandle = GetString(pID)))
- return -1;
-
- HLock(aStringHandle);
- aStringPtr = *aStringHandle;
-
- for(aStringLength = *aStringPtr++; aStringLength > 0; aStringLength--)
- {
- aChar = *aStringPtr++;
-
- /* "\"mark procedure */
- if(aYenFlag)
- {
- aYenFlag = false;
- switch(aChar)
- {
- case 'Q':
- case 'q':
- Scanf();
- aCRflag = true;
- break;
- case 'N':
- case 'n':
- aCRflag = true;
- break;
- }
- }else{
- if('\\' == aChar)
- {
- aYenFlag = true;
- }else{
- DrawChar(aChar);
- }
- }
-
- /* linefeed performance */
- if(aCRflag)
- {
- aCRflag = false;
- MoveTo(aXpos,aYpos += aVpitch);
- }
- }
- HUnlock(aStringHandle);
-
- return 0;
- }
-
- void TextDrawClass::Scanf(void)
- {
- /* local variables */
- EventRecord aEvent;
- Boolean aLoop;
- BlinkCursorClass *aCursor;
- int aKeyCode;
-
- /* procedures */
-
- /* flush the events */
- FlushEvents(everyEvent,0);
-
- /* make BlinkCursor object */
- if(aCursor = new(BlinkCursorClass))
- aCursor->Init(GetPreset1(BLINK_TIME_ID),GetPreset2(CURSOR_SIZE_ID));
-
- for(aLoop = true; aLoop; )
- {
- /* display cursor blink */
- if(aCursor)
- aCursor->Idle();
-
- /* get key event */
- if(GetOSEvent(keyDownMask,&aEvent))
- {
- /* hide cursor */
- if(aCursor)
- aCursor->Hide();
-
- /* linefeed performance or key_punch performance */
- switch(aKeyCode = (int)(charCodeMask & aEvent.message))
- {
- case 0x0d: /* Return key */
- case 0x03: /* Enter key */
- aLoop = false;
- break;
-
- default: /* display ASCII code */
- if(0x20 <= aKeyCode && aKeyCode < 0x7f)
- {
- DrawChar(aKeyCode);
- }
- break;
- }
- }
- }
-
- /* destroy BlinkCursor object */
- if(aCursor)
- delete(aCursor);
- }
-
- /*--------------------------------------------------------------------------
- public program
- --------------------------------------------------------------------------*/
-
- void main(void)
- {
- /* local variables */
- Handle aThisHandle; /* Handle to This INIT resource */
- Handle aProcHandle; /* Handle to PROC resource */
- GrafPort myPort;
- QuickDraw qdGlobals;
- Ptr localA5;
- Ptr savedA5;
-
- /* procedures */
-
- /* prepare to use code resource */
- RememberA0(); /* to access resource globals */
- SetUpA4();
- asm {
- _RecoverHandle;
- move.l a0,aThisHandle
- }
- HLock(aThisHandle);
-
- /* perform ,if No shift key pressed */
- if(!IsShiftPress())
- {
- /* local variables */
- Rect aRect;
- SaveBitMapType aSaveScreen; /* buffer to escape images */
- int aSaveFlag; /* result of SaveBitMap */
-
- /* keep the own graph port */
- asm
- {
- move.l A5,savedA5
- lea localA5,A5
- move.l A5,CurrentA5
- }
- InitGraf(&qdGlobals.thePort);
- OpenPort(&myPort);
-
- /* keep the own QuickDraw global variable */
- gQD = &qdGlobals;
-
- /* save the images of CRT */
- aSaveScreen.sBitMap = &(gQD->screenBits);
- aRect = aSaveScreen.sBitMap->bounds;
- aSaveScreen.left = aRect.left;
- aSaveScreen.right = aRect.right;
- aSaveScreen.top = aRect.top;
- aSaveScreen.bottom = aRect.bottom;
- aSaveFlag = SaveBitMap(&aSaveScreen);
-
- /* black out the CRT */
- FillRect(&aRect,gQD->black);
-
- /* change TEXT mode and Font mode */
- InitFonts();
- TextFont(1); /* set font mode as application font */
- TextMode(srcXor);
-
- /* This is the main procedure!! */
- RealMain();
-
- /* recover the images of CRT */
- if(0 == aSaveFlag)
- RestoreBitMap(&aSaveScreen);
-
- /* close the graph port */
- ClosePort(&myPort);
- asm
- {
- move.l savedA5,A5
- move.l A5,CurrentA5
- }
- }
-
- /* exit from this code resource */
- HUnlock(aThisHandle);
- RestoreA4();
- }
-
- void RealMain(void)
- {
- /* local variables */
- TextDrawClass *aTextDraw; /* text draw and response object */
- int aID; /* ID number of STR resource */
-
- /* procedures */
-
- /* make a text draw object */
- if(NULL == (aTextDraw = new(TextDrawClass)))
- return;
- aTextDraw->Init(GetPreset2(START_POINT_ID),GetPreset1(VERTICAL_PITCH_ID));
-
- /* perform and response */
- aID = MESSAGE_START_ID;
- while(0 == aTextDraw->Perform(aID++))
- Wait();
-
- /* destroy a text draw object */
- delete(aTextDraw);
- }
-
- /*--------------------------------------------------------------------------
- private program
- --------------------------------------------------------------------------*/
-
- /*
- get a value of STR resource
- return 0 if any error occurred
- */
- int GetPreset1(int pID)
- {
- /* local variable */
- Handle aHandle;
-
- /* procedure */
- if(NULL == (aHandle = GetResource('STR ',pID)))
- return 0;
- return GetPresetSub(aHandle,0);
- }
-
- /*
- get a coordinate of STR resource
- return 0,0 if any error occurred
- */
- Point GetPreset2(int pID)
- {
- /* local variable */
- Handle aHandle;
- Point aPoint;
-
- /* procedure */
- if(NULL == (aHandle = GetResource('STR ',pID)))
- {
- SetPt(&aPoint,0,0);
- }else{
- SetPt(&aPoint,GetPresetSub(aHandle,0),GetPresetSub(aHandle,1));
- }
- return aPoint;
- }
-
- /*
- convert STR resource which specified by pHandle To number
- pSkip means skip counts of ','mark
- return 0 if any error occurred
- */
- static int GetPresetSub(Handle pHandle,int pSkip)
- {
- /* local variable */
- int aResult = 0;
- int aCount;
- char *aPtr,aChar;
- int aPlusMinus = 1;
-
- /* procedure */
- aPtr = *((char **)pHandle);
- for(aCount = *aPtr++; aCount > 0; aCount--,aPtr++)
- {
- if(pSkip > 0)
- {
- if(*aPtr == ',')
- --pSkip;
- }else{
- switch(aChar = *aPtr)
- {
- case '-':
- aPlusMinus = -1;
- break;
- default:
- if('0' <= aChar && aChar <= '9')
- aResult = aChar - '0' + aResult * 10;
- else
- aCount = 0;
- break;
- }
- }
- }
- return aPlusMinus * aResult;
- }
-
- /*
- save images of CRT
- return 0 if succeeded,-1 if failed
- parameters which you must setup is
- BitMap *sBitMap; bitmap definition of source images
- int left; left posision of source images
- int top; top posision of source images
- int right; right posision of source images
- int bottom; bottom posision of source images
- */
- int SaveBitMap(SaveBitMapType *pSaveBitMap)
- {
- /* local variable */
- /* int aRowBytes; */
- Rect aSourceRect,aDestRect;
- /* Ptr aPtr; */
- BitMap *aBitMap;
-
- /* procedure */
- aBitMap = &pSaveBitMap->dBitMap;
-
- SetRect(&aSourceRect,
- pSaveBitMap->left,pSaveBitMap->top,
- pSaveBitMap->right,pSaveBitMap->bottom);
-
- if(0 != SetupOffBitMap(aBitMap,
- aSourceRect.right - aSourceRect.left,
- aSourceRect.bottom - aSourceRect.top))
- return -1;
-
- aDestRect = aBitMap->bounds;
-
- CopyBits(pSaveBitMap->sBitMap,
- aBitMap,
- &aSourceRect,
- &aDestRect,
- srcCopy,
- NULL
- );
-
- return 0;
- }
-
- /*
- recover images which saved by SaveBitMap
- pSaveBitMap parameter is same parameter which used by SaveBitMap
- */
- void RestoreBitMap(SaveBitMapType *pSaveBitMap)
- {
- /* local variable */
- Rect aSourceRect,aDestRect;
-
- /* procedure */
- SetRect(&aDestRect,
- pSaveBitMap->left,pSaveBitMap->top,
- pSaveBitMap->right,pSaveBitMap->bottom);
-
- SetRect(&aSourceRect,
- 0,0,
- aDestRect.right - aDestRect.left,
- aDestRect.bottom - aDestRect.top);
-
- CopyBits(&pSaveBitMap->dBitMap,
- pSaveBitMap->sBitMap,
- &aSourceRect,
- &aDestRect,
- srcCopy,
- NULL
- );
-
- DisposOffBitMap(&pSaveBitMap->dBitMap);
- }
-
- /*
- make off_screen_bit_map
- pBitMap is a structure to setup information
- ph is dots of horizontal
- pv is dots of vertical
- return 0 if succeeded,-1 if failed
- */
- static int SetupOffBitMap(BitMap *pBitMap,int ph,int pv)
- {
- /* local variable */
- register int aSize;
- int aRowBytes;
- Rect aRect;
- register Ptr aPtr;
- int aResult;
-
- /* procedure */
- SetRect(&aRect,0,0,ph,pv);
- if((aRowBytes = ph + 1) % 16)
- aRowBytes = (aRowBytes / 16 + 1) * 16;
- aRowBytes /= 8;
-
- pBitMap->rowBytes = aRowBytes;
- pBitMap->bounds = aRect;
- aSize = aRowBytes * (pv + 1);
- aPtr = NewPtr(aSize);
- pBitMap->baseAddr = aPtr;
- if(aPtr != noErr)
- {
- while(aSize--)
- *aPtr++ = 0;
- return 0;
- }else{
- return -1;
- }
- }
-
- /*
- release off_screen_bit_map
- pBitMap is a structure to setup information
- */
- static void DisposOffBitMap(BitMap *pBitMap)
- {
- DisposPtr(pBitMap->baseAddr);
- }
-
- /*
- return true if SHIFT key pressed
- */
- static Boolean IsShiftPress(void)
- {
- /* local variable */
- KeyMap aKeyMap;
-
- /* procedure */
- GetKeys(&aKeyMap);
- return 0L != (aKeyMap.Key[1] & 1L);
- }
-
- /*
- wait a little time
- */
- static void Wait(void)
- {
- /* local variable */
- static int aWaitTick = -1;
- long aDummy;
-
- /* procedure */
- if(aWaitTick < 0)
- aWaitTick = GetPreset1(DELAY_TIME_ID);
- Delay((long)aWaitTick,&aDummy);
- }
-
- /*--------------------------------------------------------------------------
- end of file
- --------------------------------------------------------------------------*/
-